package com.ks.utils.grap;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Transparency;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SUtilGrap {
	public static void main(String[] args) throws IOException {

		String inputImagePath = "F:/111.png";

		String outputPath = "F:/hb1.png";

		String text = "2019年瓯海义务招生报名系统";

		System.err.println(text.length());
		
		BufferedImage imgMap = drawTranslucentStringPic(1500, 400, 60,"");  
        File imgFile=new File("F:/111.png");  
        try {  
            ImageIO.write(imgMap, "PNG", imgFile);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        System.out.println("生成完成");  

		addTextToImage(inputImagePath, outputPath, text, new Color(231, 65, 51));

	}

	public static void addTextToImage(String inputImagePath, String outputPath,
			String text, Color color) throws IOException {

		File file = new File(inputImagePath);
		Image image = ImageIO.read(file);
		BufferedImage bi = new BufferedImage(image.getWidth(null),image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		System.err.println(bi.getWidth());
		float alpha = 1F;
		Graphics2D g2 = bi.createGraphics();
		
		// 增加下面代码使得背景透明
		bi = g2.getDeviceConfiguration().createCompatibleImage(image.getWidth(null),image.getHeight(null), Transparency.TRANSLUCENT);
		g2.dispose();
		g2 = bi.createGraphics();
		// 背景透明代码结束
		
		// 高清代码
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
		g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,	RenderingHints.VALUE_COLOR_RENDER_QUALITY);

		AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,alpha);
		g2.setComposite(ac);
		g2.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null),null);
		g2.setColor(color);
		g2.setBackground(Color.BLACK);
		Font font = new Font("微软雅黑", Font.BOLD, 60);
		g2.setFont(font);
		
		FontRenderContext frc = g2.getFontRenderContext();
		TextLayout tl = new TextLayout(text, font, frc);
		FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(font);
		int stringWidth = fm.stringWidth(text);
		Shape sha = tl.getOutline(AffineTransform.getTranslateInstance((bi.getWidth() - stringWidth) / 2, (bi.getHeight() / 2)));
		// 字体色
		
		g2.setColor(Color.white);
		g2.setStroke(new BasicStroke(12));
		g2.draw(sha);
		// 描边色
		g2.setColor(new Color(0, 94, 92));
		GradientPaint paint = new GradientPaint(0, 0, Color.BLUE, 100,120, Color.RED, true);
        g2.setPaint(paint);
        g2.drawString(text, (bi.getWidth() - stringWidth) / 2, (bi.getHeight() / 2));
//		g2.fill(sha);
		
		ImageIO.write(bi, "PNG", new FileOutputStream(outputPath));
	}
	
	public static BufferedImage drawTranslucentStringPic(int width, int height, Integer fontHeight,String drawStr)  
    {  
        try  
        {  
            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
            Graphics2D gd = buffImg.createGraphics();  
            //设置透明  start  
            buffImg = gd.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);  
            gd=buffImg.createGraphics();  
            //设置透明  end  
            gd.setFont(new Font("微软雅黑", Font.PLAIN, fontHeight)); //设置字体  
            gd.setColor(Color.ORANGE); //设置颜色  
            gd.drawRect(0, 0, width - 1, height - 1); //画边框  
            gd.drawString(drawStr, width/2-fontHeight*drawStr.length()/2,fontHeight); //输出文字（中文横向居中）  
            return buffImg;  
        } catch (Exception e) {  
            return null;  
        }  
    }  
	
}
